//
//  MPMyTaskInfoController.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/31.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "MPMyTaskInfoController.h"
#import "MPTaskInfoView.h"
#import "MPTaskStatusView.h"

@interface MPMyTaskInfoController ()<MPTaskStatusViewDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) MPTaskInfoView *infoView;
@property (nonatomic, strong) MPTaskStatusView *statusView;
@property (nonatomic, strong) MPTaskModel *model;

@end

@implementation MPMyTaskInfoController

+ (instancetype)vcWithTaskModel:(MPTaskModel *)model {
    MPMyTaskInfoController *vc = [[self alloc] init];
    vc.model = model;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavTitle:self.model.taskTypeName backItem:YES];
    [self setupViews];
    
    [self setNavRightItemWithImage:[UIImage imageNamed:@"nav_icon_delete_nor"] action:@selector(deleteCurrentTask)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithEncodeString:self.model.url]];
    [self updateDisplay];
}
- (void)updateDisplay {
    self.infoView.model = self.model;
    self.statusView.model = self.model;
}

#pragma mark - actions
- (void)cancleCurrentTask {
    [ATAlertView showTitle:@"任务取消后无法获得佣金,是否取消任务?" message:nil normalButtons:@[@"取消"] highlightButtons:@[@"确定"] completion:^(NSInteger index, NSString *buttonTitle) {
        if (index <= 0) {
            return;
        }
        [MPHttpTool GET:[NSString stringWithFormat:@"business/task/cancel/%@", self.model.Id]
                 params:nil
                success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                    [MBProgressHUD showMessage:@"取消成功"];
                    self.model.status = MPMarkStateCanceled;
                    [self updateDisplay];
                }
                failure:ATHttpFailure];
    }];
}
- (void)deleteCurrentTask {
    MPTaskModel *taskModel = self.model;
    [ATAlertView showTitle:@"是否删除任务?" message:nil normalButtons:@[@"取消"] highlightButtons:@[@"确定"] completion:^(NSInteger index, NSString *buttonTitle) {
        if (index <= 0) {
            return;
        }
        [MPHttpTool GET:[NSString stringWithFormat:@"business/task/delete/%@",taskModel.Id]
                 params:nil
                success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                    [MBProgressHUD showMessage:@"删除成功"];
                    self.model.status = MPMarkStateDelete;
                    [self goBack];
                } failure:ATHttpFailure];
    }];
}

- (void)appealCurrentTask {
    [MPHttpTool GET:[NSString stringWithFormat:@"business/task/appeal/%@",self.model.Id]
             params:nil
            success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                [MBProgressHUD showMessage:result.message];
                self.model.status = MPMarkStateAppealing;
                [self updateDisplay];
            } failure:ATHttpFailure];
}

#pragma mark - delegate
- (void)taskStatusViewDidClickAppealButton {
    [self appealCurrentTask];
}
- (void)taskStatusViewDidClickCancelButton {
    [self cancleCurrentTask];
}

#pragma mark - setViews
- (void)setupViews {
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.infoView];
    [self.view addSubview:self.statusView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.top.offset(NAVI_HIGHT);
    }];
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView);
        make.left.right.offset(0);
    }];
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.height.equalTo(@(TAB_BAR_HIGHT));
    }];
}
#pragma mark - getters
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}
- (MPTaskInfoView *)infoView {
    if (!_infoView) {
        _infoView = [[NSBundle mainBundle] loadNibNamed:@"MPTaskInfoView" owner:nil options:nil][0];
    }
    return _infoView;
}
- (MPTaskStatusView *)statusView {
    if (!_statusView) {
        _statusView = [[NSBundle mainBundle] loadNibNamed:@"MPTaskStatusView" owner:nil options:nil][0];
        _statusView.delegate = self;
    }
    return _statusView;
}

@end
